from scipy.signal import savgol_filter
from matplotlib import pyplot as plt


def find_first_minimaxmum(x_data, y_data):
    first_min_value = None
    first_max_value = None
    y_data = savgol_filter(y_data, 11, 1, mode="nearest")
    # savgol(x_data,y_data)
    # print(y_data)
    for y_index in range(len(y_data)):
        if 0 < y_index < len(y_data):
            if float(x_data[y_index]) < 0.2:
                continue
            if float(y_data[y_index - 1]) >= float(y_data[y_index]) and float(y_data[y_index + 1]) >= float(
                    y_data[y_index]) and float(y_data[y_index + 2]) >= float(
                y_data[y_index]) and float(y_data[y_index + 3]) >= float(y_data[y_index]) and float(
                y_data[y_index + 4]) >= float(y_data[y_index]) \
                    and float(y_data[y_index + 5]) >= float(y_data[y_index]) and float(y_data[y_index + 6]) >= float(
                y_data[y_index]):
                first_min_value = x_data[y_index]
            if float(y_data[y_index - 1]) <= float(y_data[y_index]) and float(y_data[y_index + 1]) <= float(
                    y_data[y_index]) and float(y_data[y_index + 2]) <= float(y_data[y_index]) and float(
                y_data[y_index + 3]) <= float(y_data[y_index]) and float(
                y_data[y_index + 4]) <= float(y_data[y_index]) and float(
                y_data[y_index + 5]) <= float(y_data[y_index]) and float(
                y_data[y_index + 6]) <= float(y_data[y_index]):
                first_max_value = x_data[y_index]

            if first_min_value is not None and first_max_value is not None:
                return first_min_value, first_max_value


def find_first_minimaxmum1(x_data, y_data):
    first_min_value = None
    first_max_value = None

    for y_index in range(len(y_data)):
        if 0 < y_index < len(y_data):
            if float(x_data[y_index]) < 0.2:
                continue
            if float(y_data[y_index - 1]) >= float(y_data[y_index]) and float(y_data[y_index + 1]) >= float(
                    y_data[y_index]) and float(y_data[y_index + 2]) >= float(
                y_data[y_index]) and float(y_data[y_index + 3]) >= float(y_data[y_index]) and float(
                y_data[y_index + 4]) >= float(y_data[y_index]) \
                    and float(y_data[y_index + 5]) >= float(y_data[y_index]) and float(y_data[y_index + 6]) >= float(
                y_data[y_index]):
                first_min_value = x_data[y_index]
            if float(y_data[y_index - 1]) <= float(y_data[y_index]) and float(y_data[y_index + 1]) <= float(
                    y_data[y_index]) and float(y_data[y_index + 2]) <= float(y_data[y_index]) and float(
                y_data[y_index + 3]) <= float(y_data[y_index]) and float(
                y_data[y_index + 4]) <= float(y_data[y_index]) and float(
                y_data[y_index + 5]) <= float(y_data[y_index]) and float(
                y_data[y_index + 6]) <= float(y_data[y_index]):
                first_max_value = x_data[y_index]
            if all([first_min_value, first_max_value]):
                return first_min_value, first_max_value


# 平滑曲线
def savgol(x_data, y_data):
    plt.plot(x_data, y_data, "k")
    y_smooth = savgol_filter(y_data, 9, 3, mode="nearest")
    plt.plot(x_data, y_smooth, "b")
    plt.xlabel("WaveLength")
    plt.ylabel("Value")
    plt.grid(True)
    plt.show()


# x_data = [0.0, 0.025, 0.05, 0.07500000000000001, 0.1, 0.125, 0.15000000000000002, 0.17500000000000002, 0.2, 0.225, 0.25, 0.275, 0.30000000000000004, 0.325, 0.35000000000000003, 0.375, 0.4, 0.42500000000000004, 0.45, 0.47500000000000003, 0.5, 0.525, 0.55, 0.5750000000000001, 0.6000000000000001, 0.625, 0.65, 0.675, 0.7000000000000001, 0.7250000000000001, 0.75, 0.775, 0.8, 0.8250000000000001, 0.8500000000000001, 0.875, 0.9, 0.925, 0.9500000000000001, 0.9750000000000001, 1.0, 1.0250000000000001, 1.05, 1.075, 1.1, 1.125, 1.1500000000000001, 1.175, 1.2000000000000002, 1.225, 1.25, 1.2750000000000001, 1.3, 1.3250000000000002, 1.35, 1.375, 1.4000000000000001, 1.425, 1.4500000000000002, 1.475, 1.5, 1.5250000000000001, 1.55, 1.5750000000000002, 1.6, 1.625, 1.6500000000000001, 1.675, 1.7000000000000002, 1.725, 1.75, 1.7750000000000001, 1.8, 1.8250000000000002, 1.85, 1.875, 1.9000000000000001, 1.925, 1.9500000000000002, 1.975, 2.0, 2.025, 2.0500000000000003, 2.075, 2.1, 2.125, 2.15, 2.1750000000000003, 2.2, 2.225, 2.25, 2.275, 2.3000000000000003, 2.325, 2.35, 2.375, 2.4000000000000004, 2.4250000000000003, 2.45, 2.475, 2.5]
# y_data = [3.1137085e-06, 3.09020996e-06, 3.08837891e-06, 3.046875e-06, 2.97393799e-06, 2.92449951e-06, 2.90527344e-06, 2.85858154e-06, 2.78015137e-06, 2.65472412e-06, 2.50152588e-06, 2.39654541e-06, 2.2744751e-06, 2.14050293e-06, 2.00805664e-06, 1.84204102e-06, 1.7074585e-06, 1.5411377e-06, 1.35864258e-06, 1.20513916e-06, 1.04248047e-06, 8.69445801e-07, 6.77642822e-07, 5.44403076e-07, 4.13818359e-07, 2.90679932e-07, 1.96594238e-07, 1.14562988e-07, 5.77697754e-08, 2.84423828e-08, 3.42102051e-08, 8.00170898e-08, 1.70196533e-07, 3.01177979e-07, 4.85595703e-07, 7.20947266e-07, 9.68933105e-07, 1.30065918e-06, 1.66900635e-06, 2.06848145e-06, 2.48291016e-06, 2.95776367e-06, 3.4777832e-06, 3.8949585e-06, 4.41650391e-06, 4.86907959e-06, 5.30639648e-06, 5.70739746e-06, 6.09771729e-06, 6.31164551e-06, 6.37481689e-06, 6.47247314e-06, 6.40930176e-06, 6.2878418e-06, 6.03393555e-06, 5.64727783e-06, 5.12908936e-06, 4.52087402e-06, 3.90014648e-06, 3.2144165e-06, 2.58148193e-06, 1.90765381e-06, 1.33666992e-06, 8.16650391e-07, 4.07562256e-07, 1.40411377e-07, 2.93273926e-08, 9.11865234e-08, 3.34655762e-07, 7.43591309e-07, 1.30187988e-06, 2.04162598e-06, 2.83294678e-06, 3.64837646e-06, 4.45159912e-06, 5.17120361e-06, 5.86029053e-06, 6.29333496e-06, 6.61621094e-06, 6.71691895e-06, 6.50177002e-06, 6.11694336e-06, 5.390625e-06, 4.43023682e-06, 3.47106934e-06, 2.46826172e-06, 1.56280518e-06, 8.11462402e-07, 2.78930664e-07, 4.01611328e-08, 1.20269775e-07, 5.35308838e-07, 1.24969482e-06, 2.23693848e-06, 3.32244873e-06, 4.46502686e-06, 5.53894043e-06, 6.40808105e-06, 6.93054199e-06, 7.06939697e-06, 6.84509277e-06]
# # y_data = 8.40606689e-07, 8.51430664e-07, 8.68653564e-07, 8.21350098e-07, 8.01269531e-07, 7.70568848e-07, 7.37182617e-07, 6.97296143e-07, 6.51000977e-07, 6.00616455e-07, 5.46539307e-07, 4.88922119e-07, 4.31335449e-07, 3.69567871e-07, 3.09295654e-07, 2.49633789e-07, 1.92932129e-07, 1.41204834e-07, 9.49401855e-08, 5.73425293e-08, 2.87780762e-08, 1.38549805e-08, 1.21765137e-08, 2.68249512e-08, 5.99975586e-08, 1.14776611e-07, 1.87530518e-07, 2.88818359e-07, 4.12719727e-07, 5.6463623e-07, 7.48718262e-07, 9.559021e-07, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 9.99145508e-07, 9.94567871e-07, 7.67883301e-07, 4.31976318e-07, 1.82189941e-07, 3.87878418e-08, 1.69067383e-08, 1.25091553e-07, 3.55834961e-07, 7.18017578e-07, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 9.9822998e-07, 9.82849121e-07, 5.31982422e-07, 1.73553467e-07, 1.74255371e-08, 7.4798584e-08, 3.50769043e-07, 8.12347412e-07, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06, 1e-06
# #
# # x_data = 0.0, 0.025, 0.05, 0.07500000000000001, 0.1, 0.125, 0.15000000000000002, 0.17500000000000002, 0.2, 0.225, 0.25, 0.275, 0.30000000000000004, 0.325, 0.35000000000000003, 0.375, 0.4, 0.42500000000000004, 0.45, 0.47500000000000003, 0.5, 0.525, 0.55, 0.5750000000000001, 0.6000000000000001, 0.625, 0.65, 0.675, 0.7000000000000001, 0.7250000000000001, 0.75, 0.775, 0.8, 0.8250000000000001, 0.8500000000000001, 0.875, 0.9, 0.925, 0.9500000000000001, 0.9750000000000001, 1.0, 1.0250000000000001, 1.05, 1.075, 1.1, 1.125, 1.1500000000000001, 1.175, 1.2000000000000002, 1.225, 1.25, 1.2750000000000001, 1.3, 1.3250000000000002, 1.35, 1.375, 1.4000000000000001, 1.425, 1.4500000000000002, 1.475, 1.5, 1.5250000000000001, 1.55, 1.5750000000000002, 1.6, 1.625, 1.6500000000000001, 1.675, 1.7000000000000002, 1.725, 1.75, 1.7750000000000001, 1.8, 1.8250000000000002, 1.85, 1.875, 1.9000000000000001, 1.925, 1.9500000000000002, 1.975, 2.0, 2.025, 2.0500000000000003, 2.075, 2.1, 2.125, 2.15, 2.1750000000000003, 2.2, 2.225, 2.25, 2.275, 2.3000000000000003, 2.325, 2.35, 2.375, 2.4000000000000004, 2.4250000000000003, 2.45, 2.475, 2.0
# #
# print(find_first_minimaxmum(x_data, y_data))
